package org.gdstash.ui.select;

import org.gdstash.db.DBSkill;
import org.gdstash.db.DBSkillTree;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;

public class SkillModifierPane extends AdjustablePanel {
    private static List<DBSkill> skills;
    private static List<DBSkill> masteries;
    private static DBSkill[] modifiedSkills;
    private JComboBox<DBSkill> cbSkills;

    public static void loadSkills() {
        if (GDStashFrame.dbConfig == null || !GDStashFrame.dbConfig.gddbInit) {
            (SkillModifierPane.modifiedSkills = new DBSkill[1])[0] = null;
            return;
        }
        SkillModifierPane.skills = DBSkill.getModifiedSkills();
        SkillModifierPane.masteries = new LinkedList<DBSkill>();
        if (SkillModifierPane.skills == null) {
            (SkillModifierPane.modifiedSkills = new DBSkill[1])[0] = null;
        } else {
            final List<DBSkillTree> masteries = DBSkillTree.getMasteryTrees();
            (SkillModifierPane.modifiedSkills = new DBSkill[SkillModifierPane.skills.size() + masteries.size() + 1])[0] = null;
            int pos = 1;
            for (final DBSkillTree tree : masteries) {
                final DBSkill mSkill = tree.getMasterySkill();
                if (mSkill != null) {
                    final Object[] args = {mSkill.getName()};
                    final String txt = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_ALL_SKILLS_MASTERY", args);
                    final DBSkill skill = new DBSkill(txt, mSkill.getSkillID());
                    SkillModifierPane.masteries.add(skill);
                    SkillModifierPane.modifiedSkills[pos] = skill;
                    ++pos;
                }
            }
            for (final DBSkill skill2 : SkillModifierPane.skills) {
                SkillModifierPane.modifiedSkills[pos] = skill2;
                ++pos;
            }
        }
    }

    static {
        SkillModifierPane.skills = null;
        SkillModifierPane.masteries = null;
        SkillModifierPane.modifiedSkills = null;
        loadSkills();
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SKILL_MODIFIER"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbSkills == null) {
            this.cbSkills = new JComboBox<DBSkill>(SkillModifierPane.modifiedSkills);
        }
        this.cbSkills.setFont(fntCombo);
        this.cbSkills.setPreferredSize(dimMax);
        this.cbSkills.setMaximumSize(dimMax);
    }

    public void updateModifiedSkills() {
        final DefaultComboBoxModel<DBSkill> model = new DefaultComboBoxModel<DBSkill>(SkillModifierPane.modifiedSkills);
        this.cbSkills.setModel(model);
    }

    public void clear() {
        this.cbSkills.getModel().setSelectedItem(null);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        final int index = this.cbSkills.getSelectedIndex();
        criteria.modifiedSkill = null;
        criteria.masteryModifySkills = false;
        final DBSkill skill = (DBSkill) this.cbSkills.getSelectedItem();
        if (skill != null) {
            criteria.modifiedSkill = skill.getSkillID();
            if (index < SkillModifierPane.masteries.size() + 1) {
                criteria.masteryModifySkills = true;
            }
        }
    }

    public SkillModifierPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setVerticalGroup(vGroup);
    }
}
